home *** CD-ROM | disk | FTP | other *** search
/ Symantec Visual Cafe for Java 2.5 / symantec-visual-cafe-2.5-database-dev-edition.iso / Visual Cafe Pro v1.0 / SOURCE.BIN / Timer.java < prev    next >
Encoding:
Java Source  |  1997-06-19  |  7.1 KB  |  311 lines

  1. package symantec.itools.util;
  2.  
  3.  
  4. import java.awt.Component;
  5. import java.awt.Event;
  6.  
  7.  
  8. /**
  9.  *
  10.  * Sets a timer to wait before an action event is posted to a component.
  11.  * The caller can specify the target component, the event to send to the
  12.  * component, and the time delay.
  13.  *
  14.  * The timer is implemented as a thread.  The one of the start(...) methods should
  15.  * be called to start the thread.
  16.  *
  17.  *
  18.  * @version 1.0, Nov 26, 1996
  19.  *
  20.  * @author    Symantec
  21.  *
  22.  */
  23.  
  24.  
  25. public class Timer
  26.     implements Runnable
  27. {
  28.     Component  target;
  29.     int        eventType;
  30.     boolean    repeat;
  31.     boolean    repeating;
  32.     boolean    execute;
  33.     Thread     thread;
  34.     int        delay;
  35.  
  36.     /**
  37.      * Create default timer.  This timer will have a 1000 millisecond delay
  38.      * and an ACTION_EVENT, and will not repeat.
  39.      *
  40.      * @param t component which is the target of the timer.  This component
  41.      *          will receive the specified event when time elapses.
  42.      *
  43.      */
  44.  
  45.     public Timer(Component t)
  46.     {
  47.         this(t, 1000);
  48.     }
  49.  
  50.     /**
  51.      * Create timer with specified delay.  This timer will use the specified
  52.      * delay and will generate an ACTION_EVENT. It will not repeat.
  53.      *
  54.      * @param t component which is the target of the timer.  This component
  55.      *          will receive the specified event when time elapses
  56.      *
  57.      * @param d delay in milliseconds
  58.      *
  59.      */
  60.  
  61.     public Timer(Component t, int d)
  62.     {
  63.         this(t, d, false);
  64.     }
  65.  
  66.     /**
  67.      * Create timer with specified delay and repeat setting.
  68.      * After the specified delay this timer will generate
  69.      * an ACTION_EVENT. It will may repeat.
  70.      *
  71.      * @param t component which is the target of the timer.  This component
  72.      *          will receive the specified event when time elapses
  73.      *
  74.      * @param d delay in milliseconds
  75.      *
  76.      * @param r reset and repeat after generating the event
  77.      *
  78.      */
  79.  
  80.     public Timer(Component t, int d, boolean r)
  81.     {
  82.         this(t, d, r, Event.ACTION_EVENT);
  83.     }
  84.  
  85.     /**
  86.      * Create timer with specified delay, repeat setting, and event.
  87.      * After the specified delay this timer will generate
  88.      * the sepecified event. It will may repeat.
  89.      *
  90.      * @param t component which is the target of the timer.  This component
  91.      *          will receive the specified event when time elapses
  92.      *
  93.      * @param d delay in milliseconds
  94.      *
  95.      * @param r reset and repeat after generating the event
  96.      *
  97.      * @param e event to send to the target component when time elapses
  98.      *
  99.      * @see java.awt.Event
  100.      *
  101.      */
  102.  
  103.     public Timer(Component t, int d, boolean r, int e)
  104.     {
  105.         target    = t;
  106.         delay     = d;
  107.         repeat    = r;
  108.         execute   = false;
  109.         thread    = new Thread(this);
  110.         eventType = e;
  111.         thread.start();
  112.     }
  113.  
  114.     /**
  115.      *  Set or change the type of event the timer will produce.
  116.      *  @param type type of event to be generated by the timer the next
  117.      *              time the delay time elapses
  118.      *
  119.      * @see java.awt.Event
  120.      */
  121.  
  122.     public void setEventType(int type)
  123.     {
  124.         eventType = type;
  125.     }
  126.  
  127.  
  128.     /**
  129.      *  Get the type of event the timer will produce.
  130.      *  @return  Type of event to be generated by the timer the next
  131.      *           time the delay time elapses.
  132.      *
  133.      * @see java.awt.Event
  134.      */
  135.  
  136.     public int getEventType()
  137.     {
  138.         return eventType;
  139.     }
  140.  
  141.     /**
  142.      *  Change the component that is the target of this timer.
  143.      *  This is the component which will receive the event when the
  144.      *  time elapses
  145.      *
  146.      * @param t component which will be the target of the timer.  This component
  147.      *          will receive the specified event when time elapses
  148.      *
  149.      */
  150.     public void setTarget(Component t)
  151.     {
  152.         target = t;
  153.     }
  154.  
  155.     /**
  156.      *  Get the component that is the target of this timer.
  157.      *
  158.      * @return The component that is currently the target of the timer.
  159.      *
  160.      */
  161.  
  162.     public Component getTarget()
  163.     {
  164.         return target;
  165.     }
  166.  
  167.     /**
  168.      * Set the delay time for this timer.
  169.      * @param d delay in milliseconds.  This delay will be used starting
  170.      *          after the current delay elapses
  171.      *
  172.      */
  173.     public void setDelay(int d)
  174.     {
  175.         delay = d;
  176.     }
  177.  
  178.     /**
  179.      * Obtain the delay time setting for this timer.
  180.      * @return The current delay setting for this timer.
  181.      *         The delay time is in milliseconds
  182.      *
  183.      */
  184.     public int getDelay()
  185.     {
  186.         return delay;
  187.     }
  188.  
  189.     /**
  190.      * Start the timer with existing settings.
  191.      */
  192.  
  193.     public void start()
  194.     {
  195.         execute = true;
  196.         thread.resume();
  197.     }
  198.  
  199.     /**
  200.      * Change the repeat setting of the timer.
  201.      * If the repeat setting is false a single event will be generated.  When
  202.      * set to true the timer produces a series of events.
  203.      *
  204.      * @param r reset and repeat after generating the event
  205.      */
  206.  
  207.     public void setRepeat(boolean f)
  208.     {
  209.         repeat = f;
  210.     }
  211.  
  212.     /**
  213.      * Obtain the repeat setting of the timer.
  214.      *
  215.      * @return true if this timer is set to repeat, false if this timer does not repeat
  216.      */
  217.  
  218.     public boolean getRepeat()
  219.     {
  220.         return repeat;
  221.     }
  222.  
  223.     /**
  224.      * Start the timer using the specified delay
  225.      *
  226.  
  227.      *
  228.      */
  229.  
  230.     public void start(int d)
  231.     {
  232.         delay = d;
  233.  
  234.         start();
  235.     }
  236.  
  237.     /**
  238.      * Start the timer using the specified repeat setting.
  239.      *
  240.      * @param r reset and repeat after generating the event
  241.      *
  242.      */
  243.  
  244.     public void start(boolean r)
  245.     {
  246.         repeat = r;
  247.  
  248.         start();
  249.     }
  250.  
  251.     /**
  252.      * Start the timer using the specified delay and repeat settings.
  253.      *
  254.      * @param d delay in milliseconds
  255.      * @param r reset and repeat after generating the event
  256.      *
  257.      */
  258.  
  259.     public void start(int d, boolean r)
  260.     {
  261.         delay  = d;
  262.         repeat = r;
  263.  
  264.         start();
  265.     }
  266.  
  267.     /**
  268.      * Stop the timer.  After return the timer will generate no more events.
  269.      */
  270.  
  271.     public void stop()
  272.     {
  273.         execute   = false;
  274.         repeating = false;
  275.     }
  276.  
  277.     /**
  278.      * Thread body.  This method is called by the Java virtual machine in response to a
  279.      * start call by the user.
  280.      * @see #start()
  281.      */
  282.  
  283.     public void run()
  284.     {
  285.         if(!execute) thread.suspend();
  286.         try
  287.         {
  288.             while(true)
  289.             {
  290.                 repeating = repeat;
  291.  
  292.                 do
  293.                 {
  294.                     thread.sleep(delay);
  295.  
  296.                     if (execute)
  297.                     {
  298.                         target.handleEvent(new Event(this, eventType, null));
  299.                     }
  300.                 }
  301.                 while (repeating);
  302.  
  303.                 thread.suspend();
  304.             }
  305.         }
  306.         catch (InterruptedException e)
  307.         {
  308.         }
  309.     }
  310. }
  311.